#include <iostream>
#include <string>
#include <vector>
#include <type_traits>

// ==================== 基类定义 ====================
class Base {
public:
    // 多个构造函数
    Base() {
        std::cout << "Base::Base()" << std::endl;
        value = 0;
        name = "default";
    }
    
    Base(int v) {
        std::cout << "Base::Base(int): " << v << std::endl;
        value = v;
        name = "int_constructor";
    }
    
    Base(const std::string& n) {
        std::cout << "Base::Base(string): " << n << std::endl;
        value = n.length();
        name = n;
    }
    
    Base(int v, const std::string& n) {
        std::cout << "Base::Base(int, string): " << v << ", " << n << std::endl;
        value = v;
        name = n;
    }
    
    // 数值模板构造函数
    template<typename T>
    Base(T t, int multiplier = 1, 
         typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr) {
        std::cout << "Base::Base(numeric template): " << t << " * " << multiplier << std::endl;
        value = static_cast<int>(t) * multiplier;
        name = "numeric_template_constructor";
    }
    
    virtual void show() const {
        std::cout << "Base: value=" << value << ", name=" << name << std::endl;
    }
    
    virtual ~Base() = default;

protected:
    int value;
    std::string name;
};

// ==================== 传统方式：手动转发构造函数 ====================
class DerivedTraditional : public Base {
public:
    // 需要手动转发每个基类构造函数
    DerivedTraditional() : Base() {
        std::cout << "DerivedTraditional additional setup" << std::endl;
    }
    
    DerivedTraditional(int v) : Base(v) {
        std::cout << "DerivedTraditional additional setup" << std::endl;
    }
    
    DerivedTraditional(const std::string& n) : Base(n) {
        std::cout << "DerivedTraditional additional setup" << std::endl;
    }
    
    DerivedTraditional(int v, const std::string& n) : Base(v, n) {
        std::cout << "DerivedTraditional additional setup" << std::endl;
    }
    
    // 数值模板构造函数也需要手动转发
    template<typename T>
    DerivedTraditional(T t, int multiplier = 1,
                      typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr) 
        : Base(t, multiplier) {
        std::cout << "DerivedTraditional additional setup" << std::endl;
    }
    
    void show() const override {
        std::cout << "DerivedTraditional: ";
        Base::show();
    }
};

// ==================== 继承构造函数方式 ====================
class DerivedInheriting : public Base {
public:
    // 继承基类所有构造函数！
    using Base::Base;  // 这一行继承了所有Base的构造函数
    
    // 自定义额外的构造函数
    DerivedInheriting(const std::vector<int>& vec) : Base(vec.size(), "vector") {
        std::cout << "DerivedInheriting custom constructor for vector" << std::endl;
    }
    
    void show() const override {
        std::cout << "DerivedInheriting: ";
        Base::show();
    }
};

// ==================== 复杂场景：多重继承 ====================
class Mixin {
public:
    Mixin(double d) : data(d) {
        std::cout << "Mixin constructor: " << d << std::endl;
    }

    // Mixin(const std::string& n) {
    //     std::cout << "Mixin::Mixin(string): " << n << std::endl;
    // }
    double getData() const { return data; }
    
private:
    double data;
};

class MultiDerived : public Base, public Mixin {
public:
    //using Base::Base;

    template<typename... Args>
    MultiDerived(double mixin_val, Args&&... args) 
        : Base(std::forward<Args>(args)...), Mixin(mixin_val) {
        std::cout << "MultiDerived constructor" << std::endl;
    }
    
    void show() const override {
        std::cout << "MultiDerived: ";
        Base::show();
        std::cout << "  Mixin data: " << getData() << std::endl;
    }
};

// ==================== 继承构造函数的限制和注意事项 ====================
class BaseWithPrivate {
private:
    BaseWithPrivate(int secret) {
        std::cout << "BaseWithPrivate private constructor" << std::endl;
    }
    
public:
    BaseWithPrivate() = default;
    BaseWithPrivate(const std::string& s) {
        std::cout << "BaseWithPrivate public constructor: " << s << std::endl;
    }
};

class DerivedFromPrivate : public BaseWithPrivate {
public:
    using BaseWithPrivate::BaseWithPrivate;  // 只继承可访问的构造函数
    
    // 注意：私有构造函数不会被继承
};

// ==================== 测试函数 ====================
void test_traditional_approach() {
    std::cout << "\n=== 传统方式测试 ===" << std::endl;
    
    DerivedTraditional d1;
    DerivedTraditional d2(42);
    DerivedTraditional d3("hello");
    DerivedTraditional d4(100, "world");
    DerivedTraditional d5(3.14, 2);
    
    d1.show();
    d2.show();
    d3.show();
    d4.show();
    d5.show();
}

void test_inheriting_constructors() {
    std::cout << "\n=== 继承构造函数测试 ===" << std::endl;
    
    // 所有基类构造函数都可以直接使用！
    DerivedInheriting d1;                    // 使用继承的 Base()
    DerivedInheriting d2(42);                // 使用继承的 Base(int)  
    DerivedInheriting d3("hello");           // 使用继承的 Base(string)
    DerivedInheriting d4(100, "world");      // 使用继承的 Base(int, string)
    DerivedInheriting d5(3.14, 2);          // 使用继承的模板构造函数
    
    // 自定义构造函数
    std::vector<int> vec = {1, 2, 3, 4, 5};
    DerivedInheriting d6(vec);
    
    d1.show();
    d2.show();
    d3.show();
    d4.show();
    d5.show();
    d6.show();
}

void test_multi_inheritance() {
    std::cout << "\n=== 多重继承测试 ===" << std::endl;
    
    MultiDerived md1(3.14, 42, "multi");
    MultiDerived md2(2.71, 5.5, 3);

    //std::string strparam = "test";
    //MultiDerived d3(strparam); 
    // 当 MultiDerived类中 没有 using Base::Base; 报错 inheriting_constructors_demo.cpp:205:18: error: no matching constructor for initialization of 'MultiDerived'
    // 有 using Base::Base 的时候，报错：inheriting_constructors_demo.cpp:205:18: error: constructor inherited by 'MultiDerived' from base class 'Base' is implicitly deleted
    // ai 解释为： 在多重继承中，如果其他基类没有默认构造函数，继承构造函数会被删除
    // Mixin 缺少默认构造函数，导致继承的 Base 构造函数无法工作！


    md1.show();
    md2.show();
}

void test_access_control() {
    std::cout << "\n=== 访问控制测试 ===" << std::endl;
    
    DerivedFromPrivate dp1;                  // OK: 继承的默认构造函数
    DerivedFromPrivate dp2("public");        // OK: 继承的公有构造函数
    // DerivedFromPrivate dp3(123);          // 错误: 私有构造函数不会被继承 //报错 error: calling a private constructor of class 'BaseWithPrivate'
}

int main() {
    test_traditional_approach();
    test_inheriting_constructors();
    test_multi_inheritance();
    test_access_control();
    
    return 0;
}
